package com.sicnu.demo05

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class StopWatchActivity : AppCompatActivity() {
    
    private lateinit var timeTextView: TextView
    private lateinit var btnStart: Button
    private lateinit var btnStop: Button
    private lateinit var btnReset: Button
    
    private var seconds = 0
    private var running = false
    private var wasRunning = false
    
    private val handler = Handler(Looper.getMainLooper())
    private val runnable = object : Runnable {
        override fun run() {
            if (running) {
                seconds++
                updateTimeText()
                handler.postDelayed(this, 1000)
            }
        }
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_stopwatch)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        
        if (savedInstanceState != null) {
            seconds = savedInstanceState.getInt("seconds")
            running = savedInstanceState.getBoolean("running")
            wasRunning = savedInstanceState.getBoolean("wasRunning")
        }
        
        timeTextView = findViewById(R.id.timeTextView)
        btnStart = findViewById(R.id.btnStart)
        btnStop = findViewById(R.id.btnStop)
        btnReset = findViewById(R.id.btnReset)
        
        updateTimeText()
        
        btnStart.setOnClickListener {
            running = true
            handler.post(runnable)
        }
        
        btnStop.setOnClickListener {
            running = false
        }
        
        btnReset.setOnClickListener {
            running = false
            seconds = 0
            updateTimeText()
        }
    }
    
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt("seconds", seconds)
        outState.putBoolean("running", running)
        outState.putBoolean("wasRunning", wasRunning)
    }
    
    override fun onPause() {
        super.onPause()
        wasRunning = running
        running = false
    }
    
    override fun onResume() {
        super.onResume()
        if (wasRunning) {
            running = true
            handler.post(runnable)
        }
    }
    
    private fun updateTimeText() {
        val hours = seconds / 3600
        val minutes = (seconds % 3600) / 60
        val secs = seconds % 60
        timeTextView.text = String.format("%02d:%02d:%02d", hours, minutes, secs)
    }
}

